<template>
  <div class="synchronous">
    <div class="plantime" v-if="showlist">
      <div class="left">

        <el-form label-width="auto" size="mini">
          <el-row>
            <el-col :span="7">
              <el-form-item label="角色名称：">
                <el-input  placeholder="请输入"
                    size="mini"
                    style="width: 250px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="角色类型：" label-width="250px">
                <el-select
                  v-model="listQuery.type"
                  :placeholder="$t('table.type')"
                  clearable
                  class="filter-item"
                  style="width: 150px"
                >
                  <el-option
                    v-for="item in calendarTypeOptions"
                    :key="item.key"
                    :label="item.display_name + '(' + item.key + ')'"
                    :value="item.key"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="right">
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
          {{ $t("table.search") }}
        </el-button>
      </div>
    </div>

    <!-- ---- -->
    <div class="contentT" v-if="showlist">
      <div class="tips">
          <el-button type="primary" @click="openDialog">新增</el-button>
          <el-dialog title="新增" :visible.sync="dialogFormVisible" width="30%"  class="custom-dialog">
       <el-form >
         <el-form-item label="角色名称：" :label-width="formLabelWidth">
           <el-input  auto-complete="off"></el-input>
         </el-form-item>
         <el-form-item label="角色类型：" :label-width="formLabelWidth">
               <el-radio v-model="radio" label="1">管理员</el-radio>
                <el-radio v-model="radio" label="2">普通用户</el-radio>
         </el-form-item>
         <el-form-item label="组织机构："  :label-width="formLabelWidth">
                <el-select
                  v-model="listQuery.type"
                  :placeholder="$t('table.type')"
                  clearable
                  class="filter-item"
                  style="width: 150px"
                >
                  <el-option
                    v-for="item in calendarTypeOptions"
                    :key="item.key"
                    :label="item.display_name + '(' + item.key + ')'"
                    :value="item.key"
                  />
                </el-select>
         </el-form-item>
         <el-form-item label="所属系统：" :label-width="formLabelWidth">
           <el-input  auto-complete="off" placeholder="请输入"></el-input>
         </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
         <el-button @click="dialogFormVisible = false">取 消</el-button>
       </div>
     </el-dialog>
      </div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        :header-cell-style="{
          background: '#EAF1FF',
          'text-align': 'center',
          color: '#000',
        }"
        :cell-style="{ 'text-align': 'center' }"
        fit
        highlight-current-row
        @sort-change="sortChange"
        style="width: 100%"
        :border="false"
      >
        <el-table-column
          :label="$t('table.id')"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
        >
          <template slot-scope="{ row }">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色名称" width="542" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.title  }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色类型" width="240">
          <template slot-scope="{ row }">
            <span>{{ row.author }}</span>
          </template>
        </el-table-column>
        <el-table-column label="组织机构" min-width="240" align="center">
          <template slot-scope="{ row }">
            <span class="link-type" @click="handleUpdate(row)">{{
              row.title
            }}</span>
            <el-tag>{{ row.type | typeFilter }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="所属系统" width="240" align="center">
          <template slot-scope="{ row }">
            <span style="color: red">{{ row.reviewer }}</span>
          </template>
        </el-table-column>
   
   

           <el-table-column
            label="操作"
            align="center"
            width="180px"
            class-name="small-padding fixed-width"
          >
     
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                
                type="text"
                size="small">关联用户</el-button>
              <el-button type="text" size="small" @click="assignment(scope.row)">权限分配</el-button>
              <el-button type="text" size="small" @click="handlerdelete()">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
                  <RelevancyUserCom  ref='dialog'/>
                   <PermissionCom ref='permDialog' />

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />

 


    </div>

   
  </div>
</template>

<script>

import {
  fetchList,
  fetchPv,
  createArticle,
  updateArticle,
} from "@/api/article";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import  RelevancyUserCom from './component/RelevancyUserCom'
import PermissionCom  from './component/permissionCom'

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" },
];

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;
  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination,RelevancyUserCom ,PermissionCom},
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    },
  },
  data() {
    return {
      showlist: true,
      showdetail: false,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
       dialogTableVisible: false,
        dialogFormVisible: false,
        dialogPermissionVisible:false,
      radio:'1',
        
      formLabelWidth: '120px',
      listQuery: {
        page: 1,
        limit: 20,
        // importance: undefined,
        // title: undefined,
        type: undefined,
        sort: "+id",
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      sortOptions: [
        { label: "ID Ascending", key: "+id" },
        { label: "ID Descending", key: "-id" },
      ],
      statusOptions: ["published", "draft", "deleted"],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        type: "",
        status: "published",
      },

      dialogStatus: "",

      pvData: [],

      downloadLoading: false,
    };
  },
  created() {
    this.getList();
  },
  mounted() {
   
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.list = response.data.items;
        this.total = response.data.total;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    openDialog(){
           this.dialogFormVisible = true;
    },
    // 关联用户
    handleClick(row){
        this.$refs.dialog.dialogTableVisible=true
  
    },
    // 权限分配
    assignment(){
      this.$refs.permDialog.dialogPermissionVisible=true
      // console.log(this.$refs.permDialog);
    },
        handlerdelete() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    },
    sortByID(order) {
      if (order === "ascending") {
        this.listQuery.sort = "+id";
      } else {
        this.listQuery.sort = "-id";
      }
      this.handleFilter();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },

    handleUpdate(row) {

      this.showlist = false;
      this.showdetail = true;
    },

    handleFetchPv(pv) {
      fetchPv(pv).then((response) => {
        this.pvData = response.data.pvData;
        this.dialogPvVisible = true;
      });
    },

    formatJson(filterVal) {
      return this.list.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },
  },
};
</script>

<style lang='scss' scoped>
@import "@/styles/Fadeinout.scss";
.synchronous {
  padding: 20px;
  .plantime {
    background-color: #fff;
    width: 100%;
    height: 64px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 20px;
    font-size: 14px;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;

    
    .left {
      margin-top: 22px;
      box-sizing: border-box;
      margin-right: 100px;
    }
  }
  .contentT {
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background-color: #fff;
    height: 100%;
    width: 100%;
    padding: 20px;

    .tips {
      width: 100%;
      height: 22px;
      font-size: 16px;
      // margin-left: 20px;
      font-weight: 500;
      color: #5084fb;
      line-height: 22px;
      margin-bottom: 20px;
                /* 在这里添加你的自定义样式 */
.custom-dialog .el-dialog__header {
  background-color: #5084FB ; /* 修改背景颜色 */

 .el-dialog__title {
            color: #fff !important;
          }
          .el-dialog__close {
            color: #fff !important;
          }
}
    .dialog-footer{
        text-align: center;
      }
    }

  
  }
}
</style>